package com.sdhs.system.syncdata.service.impl;


import cn.hutool.json.JSONUtil;
import com.banboocloud.Codec.BamboocloudFacade;
import com.sdhs.system.syncdata.common.DeptRequiredEnum;
import com.sdhs.system.syncdata.common.RoleRequiredEnum;
import com.sdhs.system.syncdata.common.UserRequiredEnum;
import com.sdhs.system.syncdata.domain.SyncSysDept;
import com.sdhs.system.syncdata.domain.SyncSysRole;
import com.sdhs.system.syncdata.domain.SyncSysUser;
import com.sdhs.system.syncdata.dto.SchemaDTO;
import com.sdhs.system.syncdata.mapper.SyncSysUserMapper;
import com.sdhs.system.syncdata.service.SchemaService;
import com.sdhs.system.syncdata.util.EntityUtil;
import com.sdhs.system.syncdata.util.ResultSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmy
 * @date 2022/9/2010:40
 * @Description:
 */
@Service
public class SchemaServiceImpl  implements SchemaService {
    @Autowired
    private SyncSysUserMapper sysUserMapper;

    @Override
    public Object getSchema(Map<String,Object> map){
        Map<String,Object> result=new HashMap<>();
        String bimRequestId= (String) map.get("bimRequestId");
        result.put("bimRequestId",bimRequestId);
        List<SchemaDTO> accountList=getFieldList(1);
//        List<SchemaDTO> roleList=getFieldList(2);
        List<SchemaDTO> deptList=getFieldList(3);
//        List<SchemaDTO> menuList=getFieldList(4);
        result.put("account",accountList);
        result.put("organization",deptList);
//        result.put("role",roleList);
//        result.put("menu",menuList);
        ResultSchema resultSchema=new ResultSchema();
        resultSchema.setBimRequestId(bimRequestId);
        resultSchema.setAccount(accountList);
//        resultSchema.setRole(roleList);
//        resultSchema.setMenu(menuList);
        resultSchema.setOrganization(deptList);
       return BamboocloudFacade.encrypt(JSONUtil.toJsonStr(resultSchema), "123456", "AES");
//        return resultSchema;
        //return new Result<>().ok(result);

    }

    @Override
    public List getFieldList(int type) {
        Object object=new Object();
        switch (type){
            case 1:
                object=new SyncSysUser();
                break;
            case 2:
                object=new SyncSysRole();
                break;
            case 3:
                object=new SyncSysDept();
                break;
//            case 4:
//                object=new SysMenu();
//                break;
        }

        List<Map<String,Object>> userFiledsInfos = EntityUtil.getFiledsInfo(object);
        //属性名
        String fieldName;
        //属性是否必填
        String fieldRequire;
        //属性类型
        String fieldType;
        List<SchemaDTO> list=new ArrayList<>();
        switch (type){
            case 1:
                for(Map<String,Object> filedsInfo:userFiledsInfos){
                    SchemaDTO schemaDTO=new SchemaDTO();
                    fieldName= (String) filedsInfo.get("name");
                    fieldType= (String) filedsInfo.get("type");
                    fieldRequire= UserRequiredEnum.getValue(fieldName);
                    if(fieldRequire!=null){
                        schemaDTO.setName(fieldName);
                        schemaDTO.setRequired(Boolean.valueOf(fieldRequire));
                        schemaDTO.setType(fieldType);
                        if("roleIds".equals(fieldName)||"deptId".equals(fieldName)){
                            schemaDTO.setMultivalued(true);
                        }else{
                            schemaDTO.setMultivalued(false);
                        }

                        list.add(schemaDTO);
                    }
                }
                break;
            case 2:
                for(Map<String,Object> filedsInfo:userFiledsInfos){
                    SchemaDTO schemaDTO=new SchemaDTO();
                    fieldName= (String) filedsInfo.get("name");
                    fieldType= (String) filedsInfo.get("type");
                    fieldRequire= RoleRequiredEnum.getValue(fieldName);
                    if(fieldRequire!=null){
                        schemaDTO.setName(fieldName);
                        schemaDTO.setRequired(Boolean.valueOf(fieldRequire));
                        schemaDTO.setType(fieldType);
                        schemaDTO.setMultivalued(false);
                        list.add(schemaDTO);
                    }
                }
                break;
            case 3:
                for(Map<String,Object> filedsInfo:userFiledsInfos){
                    SchemaDTO schemaDTO=new SchemaDTO();
                    fieldName= (String) filedsInfo.get("name");
                    fieldType= (String) filedsInfo.get("type");
                    fieldRequire= DeptRequiredEnum.getValue(fieldName);
                    if(fieldRequire!=null){
                        schemaDTO.setName(fieldName);
                        schemaDTO.setRequired(Boolean.valueOf(fieldRequire));
                        schemaDTO.setType(fieldType);
                        schemaDTO.setMultivalued(false);
                        list.add(schemaDTO);
                    }
                }
                break;
//            case 4:
//                for(Map<String,Object> filedsInfo:userFiledsInfos){
//                    SchemaDTO schemaDTO=new SchemaDTO();
//                    fieldName= (String) filedsInfo.get("name");
//                    fieldType= (String) filedsInfo.get("type");
//                    fieldRequire= MenuRequiredEnum.getValue(fieldName);
//                    if(fieldRequire!=null){
//                        schemaDTO.setName(fieldName);
//                        schemaDTO.setRequired(Boolean.valueOf(fieldRequire));
//                        schemaDTO.setType(fieldType);
//                        schemaDTO.setMultivalued(false);
//                        list.add(schemaDTO);
//                    }
//                }
//                break;
        }

        return list;
    }

}
